{% extends 'base.html' %}
{% load static %}

{% block title %}我的草稿箱 - 本地有约{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="row">
        <div class="col-lg-10 mx-auto">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4 class="mb-0"><i class="fas fa-inbox me-2"></i>我的草稿箱</h4>
                    <a href="{% url 'location:create_post' %}" class="btn btn-primary btn-sm">
                        <i class="fas fa-plus me-1"></i>新建帖子
                    </a>
                </div>
                <div class="card-body">
                    {% if drafts %}
                    <div class="list-group list-group-flush">
                        {% for draft in drafts %}
                        <div class="list-group-item d-flex justify-content-between align-items-center">
                            <div class="flex-grow-1">
                                <h6 class="mb-1">
                                    {{ draft.title }}
                                    {% if draft.status == 'reported' %}
                                    <span class="badge bg-danger ms-2">被举报</span>
                                    {% else %}
                                    <span class="badge bg-secondary ms-2">草稿</span>
                                    {% endif %}
                                </h6>
                                <p class="text-muted small mb-1">
                                    {{ draft.content|striptags|truncatechars:100 }}
                                </p>
                                <div class="text-muted small">
                                    <span class="me-3">最后更新: {{ draft.updated_at|timesince }}前</span>
                                    {% if draft.report_reason %}
                                    <span class="text-danger">举报原因: {{ draft.report_reason|truncatechars:50 }}</span>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="btn-group">
                                <a href="{% url 'location:edit_draft' draft.id %}" class="btn btn-outline-primary btn-sm">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                                {% if draft.status == 'reported' %}
                                <button class="btn btn-outline-success btn-sm" onclick="restoreDraft({{ draft.id }})">
                                    <i class="fas fa-redo"></i> 恢复
                                </button>
                                {% endif %}
                                <button class="btn btn-outline-danger btn-sm" onclick="deleteDraft({{ draft.id }})">
                                    <i class="fas fa-trash"></i> 删除
                                </button>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">草稿箱为空</h5>
                        <p class="text-muted">您还没有保存任何草稿</p>
                        <a href="{% url 'location:create_post' %}" class="btn btn-primary">
                            <i class="fas fa-edit me-2"></i>去写帖子
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<script>
async function deleteDraft(draftId) {
    if (!confirm('确定要删除这个草稿吗？此操作不可恢复。')) {
        return;
    }
    
    try {
        const response = await fetch(`/location/community/draft/${draftId}/delete/`, {
            method: 'POST',
            headers: {
                'X-CSRFToken': getCookie('csrftoken'),
                'X-Requested-With': 'XMLHttpRequest'
            }
        });
        
        const result = await response.json();
        
        if (result.status === 'success') {
            alert(result.message);
            location.reload();
        } else {
            alert('删除失败：' + result.message);
        }
    } catch (error) {
        alert('删除失败：网络错误');
        console.error('Error:', error);
    }
}

async function restoreDraft(draftId) {
    if (!confirm('确定要将这个被举报的帖子恢复发布吗？')) {
        return;
    }
    
    try {
        const response = await fetch(`/location/community/draft/${draftId}/restore/`, {
            method: 'POST',
            headers: {
                'X-CSRFToken': getCookie('csrftoken'),
                'X-Requested-With': 'XMLHttpRequest'
            }
        });
        
        const result = await response.json();
        
        if (result.status === 'success') {
            alert(result.message);
            window.location.href = `/location/community/post/${result.post_id}/`;
        } else {
            alert('恢复失败：' + result.message);
        }
    } catch (error) {
        alert('恢复失败：网络错误');
        console.error('Error:', error);
    }
}

function getCookie(name) {
    let cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        const cookies = document.cookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            const cookie = cookies[i].trim();
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
</script>
{% endblock %}